﻿using EAF.Utility;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace App.Utility
{
    /*********************** DTO模型验证**********************/

    #region 空验证 

    /// <summary>
    /// Required的实现 验证字符串不为空 建议在字段上标记字段描述 如[Display(Name = "普通输入框")]
    /// </summary>
    public class NotNullAttribute : RequiredAttribute
    {
        //返回true表示此字段通过验证
        public override bool IsValid(object value)
        {
            string stringValue = Convert.ToString(value, CultureInfo.CurrentCulture);
            return !string.IsNullOrEmpty(stringValue);
        }

        public override string FormatErrorMessage(string name)
        {
            if (ErrorMessage.HasValue())
            {
                return base.FormatErrorMessage(name);
            }
            else
            {
                return string.Format("{0} 必须填写", name);
            }
        }
    }

    /// <summary>
    /// 字符串中间不能包含空格  建议在字段上标记字段描述 如[Display(Name = "普通输入框")]
    /// </summary>
    public class NoSpaceAttribute : ValidationAttribute
    {
        private static readonly Regex noSpaceRegex = new Regex(@"^[^\s]+$", RegexOptions.Compiled);

        //返回false验证失败
        public override bool IsValid(object value)
        {
            string stringValue = Convert.ToString(value, CultureInfo.CurrentCulture);
            if (string.IsNullOrEmpty(stringValue))
            {
                return true;
            }
            return noSpaceRegex.IsMatch(stringValue);
        }

        public override string FormatErrorMessage(string name)
        {
            if (ErrorMessage.HasValue())
            {
                return base.FormatErrorMessage(name);
            }
            else
            {
                return string.Format("{0} 内容不能包括空格", name);
            }
        }
    }

    #endregion

    #region 字符串长度验证

    /// <summary>
    /// StringLength的实现 字符串长度验证  建议在字段上标记字段描述 如[Display(Name = "普通输入框")]
    /// </summary>
    public class StrMaxLenAttribute : StringLengthAttribute
    {
        private int maxLength { get; set; }
        public StrMaxLenAttribute(int maximumLength) : base(maximumLength)
        {
            maxLength = maximumLength;
        }
        ////返回false验证失败
        //public override bool IsValid(object value)
        //{
        //    return true;
        //}

        public override string FormatErrorMessage(string name)
        {
            if (ErrorMessage.HasValue())
            {
                return base.FormatErrorMessage(name);
            }
            else
            {
                return string.Format("{0} 最大长度不能超过{1}个字符！", name, maxLength);
            }
        }


    }
    #endregion

    /// <summary>
    /// 数据库是否存在
    /// </summary>
    public class HaveToDatabaseAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            return ValidationResult.Success;
        }
    }
    /// <summary>
    /// 不能与数据库重复
    /// </summary>
    public class NoRepeatToDatabaseAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            return ValidationResult.Success;
        }
    }

    /// <summary>
    /// 不为空则转换为外键值
    /// </summary>
    public class NotNullSelectToDatabaseAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            return ValidationResult.Success;
        }
    }
}
